package com.hankarun.gevrek.lib;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;

public class DataSource {
	
	private SQLiteDatabase database;
	private SQLiteHelper dbHelper;
	
	private String[] allGroupColums = { SQLiteHelper.COLUMN_ID,
			SQLiteHelper.COLUMN_GROUP };
	
	private String[] allMessageColums = { SQLiteHelper.COLUMN_ID1,
			SQLiteHelper.COLUMN_MESSAGE };
	
	public DataSource(Context context) {
		dbHelper = new SQLiteHelper(context);
	}
	
	public void open() throws SQLException {
		database = dbHelper.getWritableDatabase();
	}

	public void close() {
		dbHelper.close();
	}
	
	public void dropGroups(){
		database.delete(SQLiteHelper.TABLE_GROUPS, null, null);
	}
	
	public void deleteGroup(long id){
		//Buraya grup silme eklenecek
	}
	
	public NewsGroupName createGroup(String group) {
		ContentValues values = new ContentValues();
		values.put(SQLiteHelper.COLUMN_GROUP, group);
		long insertId = database.insert(SQLiteHelper.TABLE_GROUPS, null,
				values);
		Cursor cursor = database.query(SQLiteHelper.TABLE_GROUPS,
				allGroupColums, SQLiteHelper.COLUMN_ID + " = " + insertId, null,
				null, null, null);
		cursor.moveToFirst();
		NewsGroupName newGroup = new NewsGroupName();
		newGroup.setId(cursor.getLong(0));
		newGroup.setGroup(cursor.getString(1));
		cursor.close();
		return newGroup;
	}
	
	public List<NewsGroupName> getAllGroups() {
		List<NewsGroupName> comments = new ArrayList<NewsGroupName>();

		Cursor cursor = database.query(SQLiteHelper.TABLE_GROUPS,
				allGroupColums, null, null, null, null, null);

		cursor.moveToFirst();
		while (!cursor.isAfterLast()) {
			NewsGroupName comment = new NewsGroupName();
			comment.setId(cursor.getLong(0));
			comment.setGroup(cursor.getString(1));
			comments.add(comment);
			cursor.moveToNext();
		}

		cursor.close();
		return comments;
	}
	
	public List<MessageName> getAllMessage() {
		List<MessageName> comments = new ArrayList<MessageName>();

		Cursor cursor = database.query(SQLiteHelper.TABLE_MESSAGES,
				allMessageColums, null, null, null, null, null);

		cursor.moveToFirst();
		while (!cursor.isAfterLast()) {
			MessageName comment = new MessageName();
			comment.setId(cursor.getLong(0));
			comment.setGroup(cursor.getString(1));
			comments.add(comment);
			cursor.moveToNext();
		}

		cursor.close();
		return comments;
	}
	
	public MessageName createMessage(String group) {
		ContentValues values = new ContentValues();
		values.put(SQLiteHelper.COLUMN_MESSAGE, group);
		long insertId = database.insert(SQLiteHelper.TABLE_MESSAGES, null,
				values);
		Cursor cursor = database.query(SQLiteHelper.TABLE_MESSAGES,
				allMessageColums, SQLiteHelper.COLUMN_ID1 + " = " + insertId, null,
				null, null, null);
		cursor.moveToFirst();
		MessageName newGroup = new MessageName();
		newGroup.setId(cursor.getLong(0));
		newGroup.setGroup(cursor.getString(1));
		cursor.close();
		return newGroup;
	}

}
